 //
//  AppDelegate.m
//  DY_Weibo3
//
//  Created by 杨德勇 on 2016/8/17.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "YDYWelcomeViewController.h"
#import "YDYUserAccount.h"
#import "YDYNewFeatureViewController.h"
#import "YDYOauthViewController.h"
#import "YDYNewFeatureCollectionViewCell.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    self.window = [[UIWindow alloc]init];
    [self.window makeKeyAndVisible];
    self.window.rootViewController = [self defaultViewController];

    [UINavigationBar appearance].tintColor = [UIColor orangeColor];
    [UITabBar appearance].tintColor = [UIColor orangeColor];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeRootViewController:) name:YDYChangeRootViewControllerNotification object:nil];
    YDYLog(@"%@",@"".ydy_cachesAddress);
  
    return YES;
}
-(void)changeRootViewController:(NSNotification *)noti{
    
    if ([noti.object isKindOfClass:[YDYOauthViewController class]]) {
        self.window.rootViewController = [UIStoryboard storyboardWithName:NSStringFromClass([YDYWelcomeViewController class]) bundle:nil].instantiateInitialViewController;
       
    }else if([noti.object isKindOfClass:[YDYNewFeatureCollectionViewCell class]]){
        self.window.rootViewController = [UIStoryboard storyboardWithName:NSStringFromClass([YDYWelcomeViewController class]) bundle:nil].instantiateInitialViewController;
        
        
    }else if([noti.object isKindOfClass:[YDYWelcomeViewController class]]){
        
        self.window.rootViewController = [UIStoryboard storyboardWithName:@"Main" bundle:nil].instantiateInitialViewController;
    }
    
    
}
-(UIViewController *)defaultViewController{
    
    if ([YDYUserAccount isLogin]) {
        if ([self isNewVersion]) {
            //已经登陆，是新版
            return [UIStoryboard storyboardWithName:NSStringFromClass([YDYNewFeatureViewController class]) bundle:nil].instantiateInitialViewController;
        }else{
            //已经登陆，旧版
            return [UIStoryboard storyboardWithName:NSStringFromClass([YDYWelcomeViewController class]) bundle:nil].instantiateInitialViewController;
        }
    }else{
        //用户未登陆
        return [UIStoryboard storyboardWithName:@"Main" bundle:nil].instantiateInitialViewController;
    }
}
-(BOOL)isNewVersion{
    
    NSString *current = [NSBundle mainBundle]. infoDictionary[@"CFBundleShortVersionString"];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSString *sandBox = [defaults objectForKey:ydy_appVersionKey];
    if (sandBox.length ==0)sandBox = @"1.0";
    
    if ([current compare:sandBox]== NSOrderedDescending) {
        [defaults setObject:current forKey:ydy_appVersionKey];
        return YES;
    }

    return NO;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
